//
//  AppointmentViewController.m
//  ICrEAM
//
//  Created by mac on 16/10/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppointmentViewController.h"
#import "ICMHeaderView.h"
#import "ICMProgressHeaderView.h"
#import "ICMBottomBarView.h"
#import "ICMRequestManager.h"
#import "ICMCarModel.h"
#import "ICMPersonerModel.h"
#import "RepairTypeSelectView.h"
#import "ICMCreateFileView.h"
#import "ICMPropertyManager.h"
#import "SYQRCodeViewController.h"
#import "MoreCarInfoView.h"
#import "ICMSelecteItemDataView.h"
#import "UIView+Extension.h"
#import "CancelViewController.h"
@interface AppointmentViewController ()<ICMHeaderViewDelegate,ICMBottomBarViewDelegate,UIScrollViewDelegate,ICMAppointmentBasicInfoViewDelegate,ICMAppointmentMaintenanceAndPartsViewDelegate,ICMProgressHeaderViewDelegate,RepairTypeDelegate,MoreCarInfoViewDelegate,ICMSelecteStepInTimeViewDelegate,ICMCreateFileViewDelegate,ICMSelecteItemDataViewDelegate,YBPopupMenuDelegate>{
    NSString *_receptionDate;
    NSString *_defaultLisence;//带入快速建档界面的值
    NSString *_defaultVin;//带入快速建档界面的值
    NSString *car_Num;
    
    NSString *LABOUR_PRICE;
    NSString *cancelCode;
    NSArray *reasonTitleArray;
    NSArray *reasonCodeArray;
}

@property (nonatomic, strong) UIScrollView *bigScrollView;

@property (nonatomic, strong) ICMHeaderView *headerView;

@property (nonatomic, strong) ICMProgressHeaderView *progressHeaderView;

@property(nonatomic,strong) CustomRecordModel *recordModel;
@property (nonatomic, strong) ICMCarModel *carModel;



@property (nonatomic, strong) RepairTypeSelectView *repairTypeSelectView;

@property (nonatomic, copy) NSString *showMessage;
@property (nonatomic, assign) BOOL isDisAppear;//是否消失
//快速建档页面
@property (nonatomic, strong) ICMCreateFileView *CreateFileView;

//底部控制条
@property (nonatomic, strong) ICMBottomBarView *bottomBarView;

//顶部进度条字典
@property (nonatomic, strong) NSMutableDictionary *progressDic;

@property (nonatomic, assign) BOOL isPayForType;//索赔类型

@property (nonatomic, strong) NSString *qrString;//vin
//多车辆信息view
@property (nonatomic, strong) MoreCarInfoView *moreCarInfoView;

//活动页显示信息用view
@property (nonatomic, strong) UIView *actMsgBgView2;
@property (nonatomic, strong) UILabel *actMsgLabel2;

//背景
@property (nonatomic, strong) UIView *createBackgroungView;
@property (nonatomic, strong) UIView *repairTypeSelectViewBackgroungView;



@property (nonatomic, strong) CancelViewController *reasonView;
@property (nonatomic, strong) YBPopupMenu *popupMenu;

@end

@implementation AppointmentViewController
#pragma mark    单据进度信息条
- (void)addProgressHeaderView{
    [self.progressHeaderView removeFromSuperview];
    self.progressHeaderView = [ICMProgressHeaderView createProgressHeaderViewWithStateDic:self.progressDic andFrame:CGRectMake(0, 40, ScreenWidth, 66)];
    self.progressHeaderView.delegate = self;
    [self.view insertSubview:self.progressHeaderView belowSubview:self.suspendView];
}

- (void)presentVC:(NSNotification *)notification {
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];

    [self presentViewController:[notification object] animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title=@"预约";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = BackgroundColor_White;
    
    self.headerView = [ICMHeaderView createHeaderViewWithFrame:CGRectMake(0, 0, ScreenWidth, 40) andTitles:@[@"基本信息",@"活动选择",@"项目和零件"] andType:@"0"];
    self.headerView.delegate = self;
    [self.view addSubview:self.headerView];
    
    self.progressDic = [@{@"0":@""}mutableCopy];
    [self addProgressHeaderView];
    _bottomBarView = [ICMBottomBarView createBottomBarViewWithTitles:@[@"保 存",@"取消预约"] normalImages:@[[UIImage imageNamed:@"btn_save"],[UIImage imageNamed:@"取消预约"]] seletedImages:nil andFrame:CGRectMake(0, ScreenHeight-70-NavBarAndStatusHeight, ScreenWidth, 70)];
    _bottomBarView.delegate = self;
    [self.view addSubview:_bottomBarView];
    [self setupBigScrollView];
    
    self.activityView.isEditType = self.isEditType;
    self.activityView.basicOtherModel = self.basicOtherModel;
    self.activityView.carTypeView = self.basicInfoView.carTypeView;
    self.activityView.stepInTimeView = self.basicInfoView.stepInTimeView;
    self.partsView.carTypeView = self.basicInfoView.carTypeView;
    if (!self.isEditType) {
        self.appointmentNumber = nil;
        if (self.basicInfoModel.LICENSE || self.basicInfoModel.VIN) {
            [self getInfoWithLICENSE:self.basicInfoModel.LICENSE orVin:self.basicInfoModel.VIN];
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appointmentShowMessage:) name:@"AppointmentShowMessage" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createFileMsgNotifacation:) name:@"CreateFileMsgNotifacation" object:nil];
    
    
    //头部跳转用赋值
    super.super_VIN = self.basicInfoModel.VIN;
    super.super_owner_no = self.basicInfoModel.OWNER_NO;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(payTypesWrong) name:@"p ayTypesWrong" object:nil];
    
    //弹出监控信息预约暂不需要
//    if (self.basicInfoModel.VIN) {
//        [self clickMonitorBtn];
//    }
    
    // Do any additional setup after loading the view from its nib.
}

- (void)payTypesWrong{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"维修类型索赔类和非索赔类不能同时选择!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentVC:) name:@"popoverSelectionView" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectRepairType:) name:@"RepairTypeChanged" object:nil];
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payTypesWrong) name:@"payTypesWrong" object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createFileMsgNotifacation:) name:@"CreateFileMsgNotifacation" object:nil];
    
    self.isDisAppear = NO;
}

-(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}

#pragma mark    设置包含所有信息的大界面
- (void)setupBigScrollView{
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake( 0, self.progressHeaderView.bottomEdge, ScreenWidth, ScreenHeight-176-NavBarAndStatusHeight)];
    
    self.bigScrollView.contentSize = CGSizeMake(ScreenWidth*3, ScreenHeight-240);
    //
    self.basicInfoView = [ICMAppointmentBasicInfoView createICMAppointmentBasicInfoViewWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.basicInfoView.delegate = self;
    
    self.activityView = [ICMAppointmentActivityView createSelecteActivityViewWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.VIN_FOR_ACTIVITY = [NSMutableArray array];
    self.activityView.VIN_FOR_ACTIVITY = self.VIN_FOR_ACTIVITY;
    
    self.partsView = [ICMAppointmentMaintenanceAndPartsView  createICMMaintenanceAndPartsViewWithFrame:CGRectMake(2*ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    
    
    [self.bigScrollView addSubview:self.basicInfoView];
    [self.bigScrollView addSubview:self.activityView];
    [self.bigScrollView addSubview:self.partsView];
    //
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.delegate = self;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    
    self.partsView.repairTypes = self.selectedRepairTypes;
    self.partsView.delegate = self;
    self.partsView.modelCode = self.basicInfoModel.MODEL_CODE;
    
    [self.view addSubview:self.bigScrollView];
    
    if (self.isEditType) {
        [self setupAllSubViewsWithModels];
    }
}

#pragma mark    快速建档界面
- (void)setupCreateFileView{
    _isRetainer = NO;
    self.CreateFileView = [ICMCreateFileView creatICMCreateFileViewWithFrame:CGRectMake((ScreenWidth-ActualWidth(940))/2, (ScreenHeight-ActualHeight(490))/2, ActualWidth(940), ActualHeight(490)) withType:1];
    self.CreateFileView.delegate = self;
    if (self.qrString) {
        self.CreateFileView.vinNumberView.contentText = self.qrString;
    }
    self.CreateFileView.type = 1;
//    self.CreateFileView.vinNumberView.contentText = self.basicInfoView.vinNumberView.contentText;
    
    self.CreateFileView.vinNumberView.contentText =_defaultVin;
   
    if (_defaultLisence.length>2){
        NSString *area = [_defaultLisence substringWithRange:NSMakeRange(0, 1)];
        NSString *plant = [_defaultLisence substringWithRange:NSMakeRange(1, _defaultLisence.length-1)];
        unsigned char mCode;
        sscanf([area UTF8String], "%c", &mCode);
        
        if (mCode <= 'Z' && mCode >= 'A') {
            area = [_defaultLisence substringWithRange:NSMakeRange(0, 2)];
            plant = [_defaultLisence substringWithRange:NSMakeRange(2, _defaultLisence.length-2)];
        }
        self.CreateFileView.selectAreaView.contentText = area;
        self.CreateFileView.plateNumberView.contentText = plant;
    }

    [self.view addSubview:self.blankView];
    [self.view addSubview:self.CreateFileView];
    
}
-(void)syncCustomInfoWithVin:(NSString *)vin{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (vin && [ICrEAMTool isVinText:vin]) {
        
        [parameters setObject:@"" forKey:@"LICENSE"];
        [parameters setObject:vin forKey:@"VIN"];
    }else{
        
        return;
    }
    [manager startGetRequestWithURL:QueryOwnerByVinLicense parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
       
            if ([data[@"DATA_STATUS"] integerValue] == 0){
                
                return ;
            }else{
                
                NSArray *carModelArray = data[@"TM_VEHICLE"];
                if (![carModelArray isKindOfClass:[NSArray class]]) {
                    return ;
                }
                
                if (carModelArray.count>0) {
                    ICMCarModel *carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
                    self.CreateFileView.selectAreaView.contentText=carModel.carNum;
                    self.CreateFileView.dateView.contentText=[ICrEAMTool getDateWithTimeInterval:carModel.SALES_DATE];
                    self.CreateFileView.natureView.contentText=[ICrEAMTool getCodeModelForCode:carModel.carNature].desc;
                }
                
                NSArray *personerModelArray = data[@"TM_OWNER"];
                
                if (![personerModelArray isKindOfClass:[NSArray class]]){
                    return;
                }
                if (personerModelArray.count>0) {
                    ICMPersonerModel *personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
                    self.CreateFileView.nameView.contentText=personerModel.ownerName;
                    self.CreateFileView.linkmanView.contentText=personerModel.CONTACTOR_NAME;
                    self.CreateFileView.linkmanMobelView.contentText=personerModel.CONTACTOR_MOBILE;
                    
                }
                 [MBProgressHUD showSuccess:@"该车辆信息已存在"];
                
            }
        }
        
    }];
    
}
-(void)textFiledShouldReturnWithVin:(NSString*)vin{
    [self syncCustomInfoWithVin:vin];
}
#pragma mark    根据模型设置界面
- (void)setupAllSubViewsWithModels{

    [self.basicInfoView setupAppointmentBasicInfoViewWithBasicModel:self.basicInfoModel andOtherModel:self.basicOtherModel];
    self.progressDic = [@{@"0":self.basicInfoModel.BOOKING_ORDER_NO?:@""}mutableCopy];
    [self addProgressHeaderView];
    
    self.activityView.basicOtherModel = self.basicOtherModel;
    
    self.selectedRepairTypes = [[self.basicInfoView.appointmentSubTypeView.contentText componentsSeparatedByString:@","]mutableCopy];
    [self getLABOUR_PRICE:self.basicInfoModel.MODEL_CODE];


    [self.partsView setupPartsViewWithPartsTopModel:self.partsTopModel];
    
    [self.partsView setupPartsViewWithRepairPartsArrayModel:self.partsArrayModel];
    
    [self.partsView setupPartsViewWithRepairMaintenancesModel:self.maintenancesModel];
    
    [self.partsView refreshInEditType];
    
    self.partsView.repairTypes = self.selectedRepairTypes;
    [self didSelectRepairType:nil];
    
    self.basicInfoView.ownerNameView.btnEnAble = NO;
    self.basicInfoView.ownerPhoneNumberView.btnEnAble = NO;
    self.basicInfoView.ownerNameView.userInteractionEnabled = NO;
    self.basicInfoView.ownerPhoneNumberView.userInteractionEnabled = NO;

    
}

/************UIScrollViewDelegate*******************/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
    float offsetX = scrollView.contentOffset.x;
    self.headerView.selectedIndex = offsetX/ScreenWidth;
    
    if (self.headerView.selectedIndex == 1) {
        self.haveShowActivity = YES;
        self.activityView.isPayForType = self.isPayForType;
        if (self.basicInfoView.haveChangeVIN) {
            self.basicInfoView.haveChangeVIN = NO;
            if (self.VIN_FOR_ACTIVITY.count) {
                [self getActivity];
            }
        }
        else
        {
            if(!self.activityView.activityModels || self.activityView.activityModels.count == 0)
            {
                if(self.actMsgBgView2)
                {
                    [self.actMsgBgView2 removeFromSuperview];
                }
                [self.activityView addSubview:self.actMsgBgView2];
                if(self.actMsgLabel2)
                {
                    [self.actMsgLabel2 removeFromSuperview];
                }
                [self.actMsgBgView2 addSubview:self.actMsgLabel2];
            }
        }
    }
    else
    {
        [self.actMsgBgView2 removeFromSuperview];
        [self.actMsgLabel2 removeFromSuperview];
        self.actMsgBgView2 = nil;
        self.actMsgLabel2 = nil;
    }
}

#pragma mark 活动页显示信息用主view2
- (UIView *)actMsgBgView2
{
    if(!_actMsgBgView2)
    {
        _actMsgBgView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
        _actMsgBgView2.backgroundColor = BackgroundColor_Gray;//[UIColor colorWithWhite:0.5 alpha:0.5];
    }
    return _actMsgBgView2;
}

#pragma mark 活动页显示信息用label
- (UILabel *)actMsgLabel2
{
    if(!_actMsgLabel2)
    {
        _actMsgLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.actMsgBgView2.frame.size.height/2-25, ScreenWidth, 50)];
        _actMsgLabel2.text = @"该车型暂无可参加的活动！";
        _actMsgLabel2.textAlignment = NSTextAlignmentCenter;
        //_actMsgLabel.backgroundColor = TitleColor_Dark;
        _actMsgLabel2.textColor = [UIColor blackColor];
        [_actMsgLabel2 sizeToFit];
        CGRect rect = _actMsgLabel2.frame;
        rect.origin.x = (ScreenWidth - rect.size.width)/2;
        rect.origin.y = (self.actMsgBgView2.frame.size.height - rect.size.height)/2;
        _actMsgLabel2.frame = rect;
    }
    return _actMsgLabel2;
}

/***********ICMHeaderViewDelegate*******************/
#pragma mark    点击了子界面跳转按钮
- (void)headerViewDidSelectedButtonAtIndex:(NSInteger)index{
    [self.view endEditing:YES];
    
    [self.bigScrollView scrollRectToVisible:CGRectMake(ScreenWidth*index, 0, ScreenWidth, ScreenHeight-240) animated:YES];
    
    if(index == 2){
        
        if(!self.carModel){
            
            [self getLABOUR_PRICE:self.recordModel.MODEL_CODE];
        
        }else{
            [self getLABOUR_PRICE:self.carModel.carType];
        
        }
    }


    if (index == 1) {
        self.haveShowActivity = YES;
        self.activityView.isPayForType = self.isPayForType;
        if (self.basicInfoView.haveChangeVIN) {
            self.basicInfoView.haveChangeVIN = NO;
            if (self.VIN_FOR_ACTIVITY.count) {
                [self getActivity];
            }
        }
        else
        {
            if(!self.activityView.activityModels || self.activityView.activityModels.count == 0)
            {
                if(self.actMsgBgView2)
                {
                    [self.actMsgBgView2 removeFromSuperview];
                }
                [self.activityView addSubview:self.actMsgBgView2];
                if(self.actMsgLabel2)
                {
                    [self.actMsgLabel2 removeFromSuperview];
                }
                [self.actMsgBgView2 addSubview:self.actMsgLabel2];
            }
        }
    }
    else{
        
        
        [self.actMsgBgView2 removeFromSuperview];
        [self.actMsgLabel2 removeFromSuperview];
        self.actMsgBgView2 = nil;
        self.actMsgLabel2 = nil;
    }
}

/************ICMBottomBarViewDelegate*******************/
#pragma mark    点击了底部功能按钮
- (void)bottomBarViewDidSelectIndex:(NSInteger)index{

    if (index == 0) {
        //保存预约单
        if (!self.haveShowActivity && self.activityView.selectedActivitys.count > 0) {
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该车辆可参加服务活动，请检查！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alter show];
            return;
        }
        [self saveAppointment];
    }else{
        //取消预约单
//        [self addReasonView];
        
        
        
        if (self.isEditType||self.isSave) {
            NSDictionary *dic = [NSDictionary dictionary];
            if([ICrEAMTool isVW]){
               //vw
                dic = @{@"BOOKING_ORDER_NO":self.basicInfoModel.BOOKING_ORDER_NO
                        };
                
            }else{
                //skd
                NSArray *reasonArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumCancelType)];
                reasonTitleArray =[ICrEAMTool getParameterTitleArrayWithArray:reasonArray];
                reasonCodeArray =[ICrEAMTool getParameterCodeArrayWithArray:reasonArray];
                
                [YBPopupMenu showRelyOnView:self.bottomBarView.bottomButtons[index] titles:reasonTitleArray icons:nil menuWidth:500 otherSettings:^(YBPopupMenu *popupMenu) {
                    popupMenu.priorityDirection = YBPopupMenuPriorityDirectionTop;
                    popupMenu.delegate = self;
                    _popupMenu = popupMenu;
                }];

                if(IsStrEmpty(cancelCode)){
                
                    return;
                }
                dic = @{@"BOOKING_ORDER_NO":self.basicInfoModel.BOOKING_ORDER_NO,
                    @"CANCLE_IN_REASON":cancelCode
                    };
            
        }

            [[[ICMRequestManager alloc]init] startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/cancelBookingOrder" parameters:dic inHudView:self.view andResultBlock:^(id data, NSString *failText) {
                if (data) {
                    [self.view showString:@"预约单取消成功!" forSeconds:1.5];
                    [_popupMenu dismiss];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidChangeAppointmentToGetCar" object:self userInfo:@{@"BOOKING_ORDER_NO":self.basicInfoModel.BOOKING_ORDER_NO}];
                    
                    self.appointmentNumber = nil;
                    self.progressDic = [@{@"0":@""}mutableCopy];
                    [self addProgressHeaderView];
                    
                    [ICrEAMTool savaOperationReceiptUserName:self.basicOtherModel.CONTACTOR_NAME carSeries:!StrIsZero_gdm(self.basicInfoModel.SERIES_NAME)?self.basicInfoModel.SERIES_NAME:self.carModel.SERIES_NAME?:@"" licence:self.basicInfoModel.LICENSE status:@"预约已取消" returnTime:@""];
                    
                }else{
                    [self.view showString:@"预约单取消失败!" forSeconds:1.5];
                }
            }];
        }else{
            [self.view showString:@"预约单还没有保存,不能取消!" forSeconds:1.5];
        }
    }
}
#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    NSLog(@"点击了 %@",reasonCodeArray[index]);
    cancelCode = reasonCodeArray[index];
    [self bottomBarViewDidSelectIndex:1];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        self.basicInfoView.delegate = nil;
        if (alertView.tag == 2233) {
            [self.navigationController popViewControllerAnimated:YES];
            return;
        }
        [self saveAppointment];
    }
    
    
}

//点击监控信息按钮
- (void)clickMonitorBtn{
    if ((self.carModel.vinNum && ![self.carModel.vinNum isEqualToString:@""]) || (self.basicInfoModel.VIN && ![self.basicInfoModel.VIN isEqualToString:@""])) {
        MonitorViewController *mvc = [[MonitorViewController alloc] init];
        StrIsZero_gdm(self.basicInfoModel.VIN)? (mvc.vin = self.carModel.vinNum) : (mvc.vin = self.basicInfoModel.VIN);
        mvc.modalPresentationStyle=UIModalPresentationFormSheet;
        [self presentViewController:mvc animated:YES completion:nil];
    }
}
//点击车辆信息
- (void)clickCarInfoBtn{
    if (self.carModel.vinNum && self.personerModel.OWNER_NO) {
        [self getInfoWithOWNER_NO:self.personerModel.OWNER_NO orVin:self.carModel.vinNum type:@"1"];
    }
}
//点击车主信息
- (void)clickCarOwnerBtn{
    if (self.carModel.vinNum && self.personerModel.OWNER_NO) {
        [self getInfoWithOWNER_NO:self.personerModel.OWNER_NO orVin:self.carModel.vinNum type:@"0"];
    }
}
#pragma mark    点击维修类型
- (void)clickRepairTypeBtn{
//    [self.repairTypeSelectViewBackgroungView addSubview:self.repairTypeSelectView];
//    [Window addSubview:self.repairTypeSelectViewBackgroungView];
////    [Window addSubview:self.repairTypeSelectView];
    [self.blankView addSubview:self.repairTypeSelectView];
    [Window addSubview:self.blankView];
}



//保存预约单
- (void)saveAppointment{
    if (![self checkDataIsComplete]) {
        NSString *showMessage = [NSString stringWithFormat:@"%@为必填项!",self.showMessage];
        [self.view showString:showMessage forSeconds:1.5];
        return;
    }
    [self startToSaveAppointmentOrderToServer];
}

//表单 check
- (BOOL)checkFormData{
    //去空格
    NSString *OWNER_MOBILE = self.basicInfoModel.OWNER_MOBILE;
    NSString *DELIVERER_MOBILE = self.basicInfoModel.DELIVERER_MOBILE;
    self.basicInfoModel.OWNER_MOBILE = [ICrEAMTool trimWhitespace:OWNER_MOBILE];
    self.basicInfoModel.DELIVERER_MOBILE = [ICrEAMTool trimWhitespace:DELIVERER_MOBILE];
    
    self.basicInfoView.ownerPhoneNumberView.contentText = [ICrEAMTool trimWhitespace:self.basicInfoView.ownerPhoneNumberView.contentText];
    self.basicInfoView.customerNameView.contentText = [ICrEAMTool trimWhitespace:self.basicInfoView.customerNameView.contentText];
    
    if([self.basicInfoModel.OWNER_PROPERTY isEqualToString:@"11901002"]){
        self.basicInfoModel.OWNER_NAME = [ICrEAMTool trimWhitespace:self.basicInfoView.ownerNameView.contentText];
        self.basicInfoModel.DELIVERER = [ICrEAMTool trimWhitespace:self.basicInfoView.customerNameView.contentText];
        self.basicInfoView.ownerNameView.contentText = [ICrEAMTool trimWhitespace:self.basicInfoView.ownerNameView.contentText];
        self.basicInfoView.customerNameView.contentText = [ICrEAMTool trimWhitespace:self.basicInfoView.customerNameView.contentText];
        
    }

    if([self.basicInfoModel.OWNER_PROPERTY isEqualToString:@"11901002"]){
        if([ICrEAMTool isVaildRealName:self.basicInfoView.ownerNameView.contentText] == NO ){
            
            [self.view showString:@"车主姓名有误，请修改后保存！" forSeconds:1.5];
            return NO;
        }
    }
    if([ICrEAMTool validateMobile:self.basicInfoView.ownerPhoneNumberView.contentText] == NO){
        
        [self.view showString:@"车主手机号有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }

    if([self.basicInfoModel.OWNER_PROPERTY isEqualToString:@"11901001"]){
        if([ICrEAMTool isVaildCompanyName:self.basicInfoView.ownerNameView.contentText] == NO ){
            
            [self.view showString:@"公司名称有误，请修改后保存！" forSeconds:1.5];
            return NO;
        }
    }
    
    if([ICrEAMTool isVaildRealName:self.basicInfoView.customerNameView.contentText] == NO ){
        
        [self.view showString:@"预约人姓名有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }
    if([ICrEAMTool validateMobile:self.basicInfoView.customerPhoneNumberView.contentText] == NO){
        
        [self.view showString:@"预约人手机号有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }

    if(!self.basicInfoModel.DELIVERER || [self.basicInfoModel.DELIVERER isEqualToString:@""])
    {
        [self.view showString:@"预约人为空，请检查!" forSeconds:1.5f];
        return NO;
    }
    if(!self.basicInfoModel.DELIVERER_MOBILE || [self.basicInfoModel.DELIVERER_MOBILE isEqualToString:@""])
    {
        [self.view showString:@"预约人号码为空，请检查!" forSeconds:1.5f];
        return NO;
    }

    if (![self.basicInfoView.selectAreaView.contentText isEqualToString:@"无"]) {
        if(!self.basicInfoModel.LICENSE || [self.basicInfoModel.LICENSE isEqualToString:@""])
        {
            [self.view showString:@"车牌为空，请检查!" forSeconds:1.5f];
            return NO;
        }
//        if(self.basicInfoModel.LICENSE && self.basicInfoModel.LICENSE.length != 7)
//        {
//            [self.view showString:@"车牌有误，请检查!" forSeconds:1.5f];
//            return NO;
//        }
    }
    
    return YES;
}

//保存预约单接口
- (void)startToSaveAppointmentOrderToServer{
    
    NSMutableDictionary *paraDic = [self setupSaveAppointmentData];
    
    if(![self checkFormData])
    {
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"/INFO_ICREAM_WEB/App/saveBookingOrder"];
    [[[ICMRequestManager alloc]init]startPostRequestWithURL:url parameters:paraDic inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if (!failText) {
            //预约时间校验不成功
            if ([data[@"CODE"] integerValue] == -200) {
                [self.view showString:data[@"ERROR_MSG"] forSeconds:1.5];
                return;
            }
            //保存成功
            [self.view showString:@"预约单保存成功!" forSeconds:1.5f];
            [ICrEAMTool savaOperationReceiptUserName:self.basicInfoModel.DELIVERER carSeries:!StrIsZero_gdm(self.basicInfoModel.SERIES_NAME)?self.basicInfoModel.SERIES_NAME:self.carModel.SERIES_NAME?:@"" licence:self.basicInfoModel.LICENSE status:@"预约未进站" returnTime:@""];
//            [self.partsView setAllStateToInit];
            self.isSave = YES;
            self.appointmentNumber = data[@"BOOKING_ORDER_NO"];
            
            if ([ICrEAMTool isVW]) {
                self.basicInfoView.isOnline = [data[@"RES_TYPE"] isEqualToString:@"ONLINE"];
            }else{
                self.basicInfoView.selecteStepInTimeView.isEditType = YES;
            }
            
            self.progressDic = [@{@"0":self.appointmentNumber?:@""}mutableCopy];
            [self addProgressHeaderView];
            //保存成功后不能再保存
//            [self.bottomBarView setButtonDisEnable:0];
            
            self.maintenancesModel = [ICMRepairMaintenancesModel yy_modelWithDictionary:data];
            [self.maintenancesModel createRepairMaintenanceBookingModel];
            
            self.partsArrayModel = [ICMRepairPartsArrayModel yy_modelWithDictionary:data];
            [self.partsArrayModel createRepairPartsBookingModel];
            
            if (self.partsArrayModel.TT_BOOKING_ORDER_PART.count) {
                [self.partsView setupPartsViewWithRepairPartsArrayModel:self.partsArrayModel];
            }
            if (self.maintenancesModel.TT_BOOKING_ORDER_LABOUR.count) {
                [self.partsView setupPartsViewWithRepairMaintenancesModel:self.maintenancesModel];
            }
        }else{
            //保存失败
            [self.view showString:@"预约单保存失败!" forSeconds:1.5f];
        }
    }];
}

//获取所有页面当中的数据保存到model当中然后进行提交
- (void)getAllDataModels{
    
    NSString *series_name = self.basicInfoModel.SERIES_NAME;
    NSString *CUS_RECORD_ID = self.basicInfoModel.CUS_RECORD_ID;
    
    
    
    self.basicInfoModel = [[ICMAppointmentBasicInfoModel alloc]init];
    [self.basicInfoModel setupBasicInfoModelWithAppointmentView:self.basicInfoView];
    self.basicInfoModel.SERIES_NAME = series_name;
    self.basicInfoModel.CUS_RECORD_ID = CUS_RECORD_ID;

    self.basicOtherModel = [[ICMAppointmentOtherInfoModel alloc]init];
    
    [self.basicOtherModel setupAppointmentOtherInfoModelWithBasicInfoView:self.basicInfoView];
    
    NSString *activityCodes;
    if (self.activityView.selectedActivitys.count){
        NSMutableArray *codes = [NSMutableArray array];
        for (ICMActivityItemModel *model in self.activityView.selectedActivitys) {
            [codes PACA_AddObject:model.ACTIVITY_CODE];
        }
        activityCodes = [codes componentsJoinedByString:@";"];
    }
    self.basicOtherModel.JOIN_ACTIVITY_CODE = activityCodes?:@"";
    
    self.partsTopModel = [[ICMPartsTopModel alloc]init];
    [self.partsTopModel setupPartsTopModelWithAppointmentPartsView:self.partsView];
    
    self.activityItemModel = [[ICMActivityItemModel alloc]init];
    [self.activityItemModel setupActivityItemModelWithAppointmentActivityView:self.activityView];
    
    self.maintenancesModel = [[ICMRepairMaintenancesModel alloc]init];
    [self.maintenancesModel setupRepairMaintenancesModelWithAppointmentPartsView:self.partsView];
    
    self.partsArrayModel = [[ICMRepairPartsArrayModel alloc]init];
    [self.partsArrayModel setupRepairPartsArrayModelWithAppointmentPartsView:self.partsView];
}

#pragma mark    保存时检查数据是否完整
- (BOOL)checkDataIsComplete{
    BOOL isComplete = YES;
    
    
    
//    if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.vinNumberView]) {
//        self.showMessage = @"VIN";
//        isComplete = NO;
//    }else
        if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.selectAreaView]){
        self.showMessage = @"车牌号";
        isComplete = NO;
    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.plateNumberView]){
        self.showMessage = @"车牌号";
        if ([self.basicInfoView.selectAreaView.contentText isEqualToString:@"无"]) {
            isComplete = YES;
        }else{
            isComplete = NO;
        }
    }
    
//      else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.ownerNameView]){
//        self.showMessage = @"车主姓名";
//        isComplete = NO;
//    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.invoiceDateView]){
//        self.showMessage = @"购车发票日期";
//        isComplete = NO;
//    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.currentMileageView]){
//        self.showMessage = @"当前行驶里程";
//        isComplete = NO;
//    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.carTypeView]){
//        self.showMessage = @"车型";
//        isComplete = NO;
//    }else
    if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.customerNameView]){
        self.showMessage = @"预约人姓名";
        isComplete = NO;
    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.customerPhoneNumberView]){
        self.showMessage = @"预约人手机";
        isComplete = NO;
    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.appointmentSubTypeView]){
        self.showMessage = @"预约业务子类型";
        isComplete = NO;
    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.stepInTimeView]){
        self.showMessage = @"预约进站时间";
        isComplete = NO;
    }else if ([ICrEAMTool isEmptyForContentText:self.basicInfoView.serviceAdvisorView]){
        self.showMessage = @"预约服务顾问";
        isComplete = NO;
    }else if ([self.basicInfoView.appointmentSubTypeView.contentText containsString:@"常规保养"]&&[ICrEAMTool isEmptyForContentText:self.basicInfoView.stationView]){
        self.showMessage = @"预约工位";
        isComplete = NO;
    }else if ([self.basicInfoView.appointmentSubTypeView.contentText containsString:@"常规保养"]&&[ICrEAMTool isEmptyForContentText:self.basicInfoView.workTimeView]){
        self.showMessage = @"预计施工时间";
        isComplete = NO;
    }
//    else if ((!self.carModel.carType || [self.carModel.carType isEqualToString:@""] || !self.carModel.carTypeName || [self.carModel.carTypeName isEqualToString:@""]) && isComplete){
//        [self.view showString:@"车型信息不完整，请维护车主车辆信息!" forSeconds:3];
//    }
    return isComplete;
}

//构造保存预约单接口数据
- (NSMutableDictionary *)setupSaveAppointmentData{
    
    [self getAllDataModels];
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    
    NSMutableDictionary *basicInfoDic = [NSMutableDictionary dictionary];
    NSDictionary *basicInfoModelDic = [self.basicInfoModel yy_modelToJSONObject];
    NSDictionary *basicOtherModelDic = [self.basicOtherModel yy_modelToJSONObject];
    NSDictionary *partsTopModelDic = [self.partsTopModel yy_modelToJSONObject];
    NSDictionary *activityItemModelDic = [self.activityItemModel yy_modelToJSONObject];
    
    [basicInfoDic addEntriesFromDictionary:basicInfoModelDic];
    [basicInfoDic addEntriesFromDictionary:basicOtherModelDic];
    [basicInfoDic addEntriesFromDictionary:partsTopModelDic];
    [basicInfoDic addEntriesFromDictionary:activityItemModelDic];

    self.recordModel.CONTACTOR_NAME = self.basicInfoView.customerNameView.contentText;
    
    if(self.recordModel){
        NSDictionary *customerModelDic = [self.recordModel yy_modelToJSONObject];
        [dataDic setObject:@[customerModelDic] forKey:@"TM_CUSTOMER_RECORD"];
        [dataDic  addEntriesFromDictionary:dataDic];
        [basicInfoDic addEntriesFromDictionary:customerModelDic];

    }
   
    [dataDic setObject:@[basicInfoDic] forKey:@"TT_BOOKING_ORDER"];
    
    NSDictionary *maintenancesModelDic = [self.maintenancesModel yy_modelToJSONObject];
    
    [dataDic addEntriesFromDictionary:maintenancesModelDic];
    
    NSDictionary *partsArrayModelDic = [self.partsArrayModel yy_modelToJSONObject];
    
    [dataDic addEntriesFromDictionary:partsArrayModelDic];
    
    return dataDic;
}

#pragma mark    车牌号VIN编辑完成之后
- (void)licenseDidEndEdit:(ICMTitleTextFeildView *)sender type:(NSString *)type{
    
    if ([type isEqualToString:@"0"]) {
        //vin
        if ([ICrEAMTool isVinText:sender.contentText]) {
            NSString *string = [sender.contentText uppercaseString];
            _defaultVin = string;
            _defaultLisence = @"";
            self.recordModel.VIN = sender.contentText;
            
            [self getInfoWithLICENSE:@"" orVin:string];
    
        }else{
            [self.view showString:@"VIN码输入有误！" forSeconds:1.5];
            return;
        }
    }else{
        //车牌号
        //获取车牌号输入框的数据
        NSString *textFieldAreaAndPlateStr = [self getTextFieldAreaAndPlate];
        
        //带入快速建档界面的值
        _defaultVin = @"";
        _defaultLisence = textFieldAreaAndPlateStr;
        
        //获取模型中的原数据
        NSString *modelAreaAndPlant = self.carModel.carNum;
        
        if (textFieldAreaAndPlateStr.length < 6) {
            [self.view showString:@"车牌号输入有误！" forSeconds:1.5];
        }else{
            if (IsStrEmpty(self.basicInfoView.vinNumberView.contentText)){
                [self getInfoWithLICENSE:textFieldAreaAndPlateStr orVin:@""];
            }
            if(_isRetainer == YES){
                [self judgeAppointmentViewState:textFieldAreaAndPlateStr];
            }else{
                [self judgeCreateViewState:textFieldAreaAndPlateStr];
            }
            
            
            
        }
    
    }
}






#pragma mark    获取车主车辆信息接口
- (void)getInfoWithLICENSE:(NSString *)LICENSE orVin:(NSString *)vin{
    if(self.moreCarInfoView)
    {
        [self.moreCarInfoView removeFromSuperview];
    }
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (LICENSE && ![LICENSE isEqualToString:@""]) {
        [parameters setObject:LICENSE forKey:@"LICENSE"];
        [parameters setObject:@"" forKey:@"VIN"];
    }else{
        [parameters setObject:@"" forKey:@"LICENSE"];
        [parameters setObject:vin forKey:@"VIN"];
    }
    [manager startGetRequestWithURL:QueryOwnerByVinLicense parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if ([data[@"DATA_STATUS"] integerValue] == 0){
                if(self.isEditType == NO){
                    [_bottomBarView setButtonEnable:0];
                    //不是编辑模式
                    //非保有客户,弹出快速登记
                    _isRetainer = NO;
                    self.basicInfoView.ownerNameView.btnEnAble = YES;
                    self.basicInfoView.ownerNameView.userInteractionEnabled = YES;
                    self.basicInfoView.ownerPhoneNumberView.btnEnAble = YES;
                    self.basicInfoView.ownerPhoneNumberView.userInteractionEnabled = YES;
                    
                    [self.basicInfoView cleanAllTextFieldText];
                    [self setupCreateFileView];
                }else{
                
                    return ;
                
                }
            }else if ([data[@"DATA_STATUS"] integerValue] == 1){
                //保有客户
                _isRetainer = YES;
                NSArray *carArray = data[@"TM_VEHICLE"];
                if (![carArray isKindOfClass:[NSArray class]]) {
                    return ;
                }
                //车辆数据模型
                self.carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
                [self getLABOUR_PRICE:self.carModel.carType];
                //售前车判断
                if ([_carModel.VEHICLE_LIFE_STATUS isEqualToString:@"25271001"]) {
                    self.carModel = [[ICMCarModel alloc]init];
                    _defaultVin = @"";
                    _defaultLisence = @"";
                    self.basicInfoView.vinNumberView.contentText = @"";
                    [_bottomBarView setButtonDisEnable:0];
                    [self alertMessage:@"该车辆为售前车辆，不支持预约和接车流程！"
                             withBlock:^{
                            [self.CreateFileView removeFromSuperview];
                             
                        }];
                    return;
                }else{
                    [_bottomBarView setButtonEnable:0];
                    //保有客户车主，车主手机不能编辑
                    self.basicInfoView.ownerNameView.btnEnAble = NO;
                    self.basicInfoView.ownerNameView.userInteractionEnabled = NO;
                    self.basicInfoView.ownerPhoneNumberView.btnEnAble = NO;
                    self.basicInfoView.ownerPhoneNumberView.userInteractionEnabled = NO;
                }

                //获取车牌号输入框的数据
                NSString *textFieldAreaAndPlateStr = [self getTextFieldAreaAndPlate];
                if([self.carModel.carNum isEqualToString:textFieldAreaAndPlateStr]){
                    NSArray *personerModelArray = data[@"TM_OWNER"];
                    if (![personerModelArray isKindOfClass:[NSArray class]]){
                        return;
                    }
                    //车主数据模型
                    self.personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
                    
                    [self getMoreCarInfoViewWithLICENSE:LICENSE andCarArray:carArray andData:data andPersonerModelArray:personerModelArray];
                    
                    if(carArray.count == 1){
                        [self resetViewAfterSelectLicense];
                    }


                }else{
                    NSArray *personerModelArray = data[@"TM_OWNER"];
                    if (![personerModelArray isKindOfClass:[NSArray class]]){
                        return;
                    }
                    //车主数据模型
                    self.personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];

                    [self judgeAppointmentViewStateOfExistAreaAndPlateStr:textFieldAreaAndPlateStr];
       
                }
            }//是否保有客户的括号
            else{
                
                if(_isEditType == YES){
                
                    return;
                }
                //非保有客户,弹出快速登记
                _isRetainer = NO;
                self.basicInfoView.ownerNameView.btnEnAble = YES;
                self.basicInfoView.ownerNameView.userInteractionEnabled = YES;
                self.basicInfoView.ownerPhoneNumberView.btnEnAble = YES;
                self.basicInfoView.ownerPhoneNumberView.userInteractionEnabled = YES;
                
                [self.basicInfoView cleanAllTextFieldText];
                
                [self setupCreateFileView];

            
            
            }
        }//code括号
    }];//请求block括号
}
//请求完覆盖界面数据
- (void)resetViewAfterSelectLicense
{
    self.basicInfoView.personModel = self.personerModel;
    self.basicInfoView.carModel = self.carModel;
    self.basicInfoView.invoiceDateView.contentText = [ICrEAMTool getDateWithTimeInterval:self.carModel.parchaseDate];
    self.basicInfoView.carTypeView.contentText = [NSString stringWithFormat:@"%@|%@",self.carModel.carType,self.carModel.carTypeName];
    self.basicInfoView.carTypeView.noStr = self.carModel.carType;
    self.basicInfoModel.MODEL_CODE = self.carModel.carType;
    self.basicInfoView.haveChangeVIN = YES;
    self.basicInfoView.ownerNameView.contentText = self.personerModel.ownerName;
    self.basicInfoView.ownerTypeView.contentText = [ICrEAMTool getCodeModelForCode:self.personerModel.ownerType].desc;
    self.basicInfoView.ownerTypeView.noStr = self.personerModel.ownerType;
    
    self.basicInfoView.ownerPhoneNumberView.contentText = self.personerModel.phoneNum;
    if (!self.isEditType) {
        self.basicInfoView.currentMileageView.contentText = self.carModel.lastMillions;
    }
    self.basicInfoView.vinNumberView.contentText = self.carModel.vinNum;
    
    
    if (self.carModel.carNum.length>2) {
        NSString *area = [self.carModel.carNum substringWithRange:NSMakeRange(0, 1)];
        NSString *plant = [self.carModel.carNum substringWithRange:NSMakeRange(1, self.carModel.carNum.length-1)];
        unsigned char mCode;
        sscanf([area UTF8String], "%c", &mCode);
        
        if (mCode <= 'Z' && mCode >= 'A') {
            area = [self.carModel.carNum substringWithRange:NSMakeRange(0, 2)];
            plant = [self.carModel.carNum substringWithRange:NSMakeRange(2, self.carModel.carNum.length-2)];
        }
        self.basicInfoView.selectAreaView.contentText = area;
        self.basicInfoView.plateNumberView.contentText = plant;
    }
    
    self.basicInfoView.invoiceDateView.contentText = [ICrEAMTool getDateWithTimeInterval:self.carModel.SALES_DATE];
    //预约信息
    if (!self.isEditType) {
        if(IsStrEmpty(self.basicInfoView.customerNameView.contentText)){
          self.basicInfoView.customerNameView.contentText = self.personerModel.CONTACTOR_NAME;
        }
        if(IsStrEmpty(self.basicInfoView.customerPhoneNumberView.contentText)){
            self.basicInfoView.customerPhoneNumberView.contentText = self.personerModel.CONTACTOR_MOBILE;
        }
        if (!self.personerModel.CONTACTOR_NAME || [self.personerModel.CONTACTOR_NAME isEqualToString:@""]) {
            self.basicInfoView.customerNameView.contentText = self.personerModel.ownerName;
        }
        if (!self.personerModel.CONTACTOR_MOBILE || [self.personerModel.CONTACTOR_MOBILE isEqualToString:@""]) {
            self.basicInfoView.customerPhoneNumberView.contentText = self.personerModel.phoneNum;
        }
    }
    
    self.partsView.workingUnitPriceView.contentText = [NSString stringWithFormat:@"%.2f",[self.carModel.LABOUR_PRICE floatValue]];
    //头部跳转用赋值
    super.super_VIN = self.carModel.vinNum;
    super.super_owner_no = self.carModel.OWNER_NO;
}


#pragma mark    跳转车主车辆信息界面
- (void)getInfoWithOWNER_NO:(NSString *)OWNER_NO orVin:(NSString *)vin type:(NSString *)type{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:OWNER_NO forKey:@"OWNER_NO"];
    [parameters setObject:vin forKey:@"VIN"];
    [manager startGetRequestWithURL:QueryOwnerDetail parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        self.carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
        self.personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
        ICMCustomerAndCarViewController *customerAndCar = [ICMCustomerAndCarViewController new];
        customerAndCar.carModel = self.carModel;
        customerAndCar.personerModel = self.personerModel;
        customerAndCar.entrance = [type integerValue];
        [self.navigationController pushViewController:customerAndCar animated:YES];
    }];
}
- (UIView *)createBackgroungView{
    if (!_createBackgroungView) {
        _createBackgroungView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _createBackgroungView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenCreateFileView)];
        [_createBackgroungView addGestureRecognizer:tap];
        
    }
    return _createBackgroungView;
}
- (UIView *)repairTypeSelectViewBackgroungView{
    if (!_repairTypeSelectViewBackgroungView) {
        _repairTypeSelectViewBackgroungView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _repairTypeSelectViewBackgroungView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenRepairTypeSelectView)];
        [_repairTypeSelectViewBackgroungView addGestureRecognizer:tap];
    }
    return _repairTypeSelectViewBackgroungView;
}

-(void)hidenRepairTypeSelectView{
    [_repairTypeSelectViewBackgroungView removeFromSuperview];
}


- (UIView *)blankView{
    if (!_blankView) {
        _blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blankView.backgroundColor = [UIColor colorWithHex:BorderColor_Gray_Hex andAlpha:0.8];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllView)];
        [_blankView addGestureRecognizer:tap];
    }
    return _blankView;
}

//- (void)hideBlankView{
////    repairTypeSelectView
//    [self.blankView removeFromSuperview];
//    if (self.CreateFileView.hidden == NO) {
//        [self.CreateFileView removeFromSuperview];
//        [self.basicInfoView cleanAllTextFieldText];
//    }
//    for (UIView *view in self.blankView.subviews) {
//        [view removeFromSuperview];
//    }
//}

- (RepairTypeSelectView *)repairTypeSelectView {
    if (!_repairTypeSelectView) {
        _repairTypeSelectView = [RepairTypeSelectView creatSelectViewWithFrame:CGRectMake(ActualWidth(207), 60, ActualWidth(610), ActualHeight(590)) andSelectedData:self.selectedRepairTypes andGetCar:NO];
        _repairTypeSelectView.delegate = self;
    }
    return _repairTypeSelectView;
}

- (NSMutableArray *)selectedRepairTypes {
    if (!_selectedRepairTypes) {
        _selectedRepairTypes = [NSMutableArray array];
    }
    return _selectedRepairTypes;
}

#pragma mark    选择了维修类型展示在界面
- (void)didSelectRepairType:(NSNotification *)notification {
    if (self.selectedRepairTypes.count) {
        NSString *typeText = [ICrEAMTool isVW]?@"常规保养0质量担保保养0一般维修0延保维修0附件安装0二手车认证维修0普通索赔0PDI 索赔0优惠索赔0受控索赔0三包维修0内部车辆维修0内部返工维修0外部返工维修0售前车辆维修0事故车维修0召回行动0技术活动0服务活动":@"常规保养0快速保养0质量担保保养0一般维修0延保维修0附件安装0二手车认证维修0普通索赔0PDI 索赔0优惠维修0受控索赔0三包维修0内部车辆维修0内部返工维修0外部返工维修0售前车辆维修0事故车维修0召回行动0技术活动0服务活动";
        NSArray *types = [typeText componentsSeparatedByString:@"0"];
        NSArray *selectedTypes = [NSArray arrayWithArray:self.selectedRepairTypes];
        [self.selectedRepairTypes removeAllObjects];
        for (NSString *type in types) {
            if ([selectedTypes containsObject:type]) {
                [self.selectedRepairTypes PACA_AddObject:type];
            }
        }
        //斯柯达预约类型非必填，不做空判断会闪退
        if(IsArrEmpty(self.selectedRepairTypes)){
        
            return;
        }
        
        NSMutableString *temp = [[NSMutableString alloc] initWithString:self.selectedRepairTypes[0]];
        for (int i = 1; i < self.selectedRepairTypes.count; i++) {
            [temp appendString:@","];
            [temp appendString:self.selectedRepairTypes[i]];
        }
        self.basicInfoView.appointmentSubTypeView.contentText = temp;
    } else {
        self.basicInfoView.appointmentSubTypeView.contentText = @"";
    }
    [self.partsView resetRepairTypeButton];
}
#pragma mark    预约单号setter方法
- (void)setAppointmentNumber:(NSString *)appointmentNumber{
    _appointmentNumber = appointmentNumber;
    BOOL haveNumber = (appointmentNumber && ![appointmentNumber isEqualToString:@""]);
    UIButton *saveButon = [self.bottomBarView.bottomButtons lastObject];
    saveButon.alpha = haveNumber?1.0:0.5;
    saveButon.userInteractionEnabled = haveNumber;
    self.basicInfoView.BOOKING_ORDER_NO = haveNumber?appointmentNumber:@"";
    self.basicInfoModel.BOOKING_ORDER_NO = self.basicInfoView.BOOKING_ORDER_NO;
}

-(void)hideView{
    
    self.blankView.hidden = YES;
}
#pragma mark    快速建档保存
- (void)clickSave:(CustomRecordModel *)model{
    self.basicInfoView.vinNumberView.contentText = model.VIN;
    self.recordModel = model;

    NSString *area = @"";
    NSString *plant = @"";
    if (model.LICENSE.length>2) {
        area = [model.LICENSE substringWithRange:NSMakeRange(0, 1)];
        plant = [model.LICENSE substringWithRange:NSMakeRange(1, model.LICENSE.length-1)];
        unsigned char mCode;
        sscanf([area UTF8String], "%c", &mCode);
        if (mCode <= 'Z' && mCode >= 'A') {
            area = [model.LICENSE substringWithRange:NSMakeRange(0, 2)];
            plant = [model.LICENSE substringWithRange:NSMakeRange(2, model.LICENSE.length-2)];
        }
    }
    self.basicInfoView.ownerNameView.contentText = model.OWNER_NAME;
    self.basicInfoView.ownerTypeView.contentText = [ICrEAMTool getCodeModelForCode:model.OWNER_PROPERTY].desc;
    self.basicInfoView.selectAreaView.contentText = area;
    self.basicInfoView.plateNumberView.contentText = plant;
    self.basicInfoView.invoiceDateView.contentText = model.SALES_DATE;
    self.basicInfoView.ownerPhoneNumberView.contentText = model.MOBILE;
    self.basicInfoView.currentMileageView.contentText = model.MILEAGE;
    self.basicInfoModel.VEHICLE_LIFE_STATUS = model.VEHICLE_LIFE_STATUS;
    self.basicInfoModel.DELIVERER = model.CONTACTOR_NAME;
    self.basicInfoModel.DELIVERER_MOBILE = model.CONTACTOR_MOBILE;
    self.basicInfoModel.CUS_RECORD_ID = model.ITEM_ID;
    
    if(IsStrEmpty(model.MODEL_CODE)){
        self.basicInfoView.carTypeView.contentText = @"";
    }else{
        self.basicInfoView.carTypeView.contentText = [NSString stringWithFormat:@"%@|%@",model.MODEL_CODE,model.MODEL_NAME];
    }
    
    self.basicInfoView.carTypeView.noStr = model.MODEL_CODE;
    self.basicInfoModel.MODEL_NAME = model.MODEL_NAME;
    self.basicInfoView.customerPhoneNumberView.contentText = model.CONTACTOR_MOBILE;
    self.basicInfoView.customerNameView.contentText = model.CONTACTOR_NAME;
    self.carModel.carType = model.MODEL_CODE;
    
    [self hideAllView];
    
    [self getLABOUR_PRICE:model.MODEL_CODE];
    
}
//移除快速建档界面
-(void)hidenCreateFileView{
    for (UIView *view in self.createBackgroungView.subviews) {
        [view removeFromSuperview];
    }
    [self.createBackgroungView removeFromSuperview];
    [self.CreateFileView removeFromSuperview];
    
}

- (void)hideAllView{
    
    [self.blankView removeFromSuperview];
    if (self.CreateFileView) {
        [self.CreateFileView removeFromSuperview];
    }
    
    for (UIView *view in self.blankView.subviews) {
        [view removeFromSuperview];
    }
    
}


- (BOOL)isPayForType{
    return self.partsView.isPayForType;
}

#pragma mark    判断能否一车相应的维修类型
- (BOOL)canRemoveItem:(NSString *)itemStr
{
    BOOL existsItems = NO;
    //检查项目
    NSArray *tempArray1 = self.maintenancesModel.TT_BOOKING_ORDER_LABOUR;
    for(int i = 0;i<tempArray1.count;i++)
    {
        ICMRepairMaintenanceModel *tempModel = tempArray1[i];
        if([tempModel.REPAIR_TYPE_NAME isEqualToString:itemStr])
        {
            existsItems = YES;
        }
    }
    if(existsItems)
    {
        [self.repairTypeSelectView showString:@"存在该维修类型的项目或零件，不能删除!" forSeconds:2];
        return NO;
    }
    //检查零件
    NSArray *tempArray2 = self.partsArrayModel.TT_BOOKING_ORDER_PART;
    for(int i = 0;i<tempArray2.count;i++)
    {
        ICMRepairPartsModel *tempModel = tempArray2[i];
        if([tempModel.REPAIR_TYPE_NAME isEqualToString:itemStr])
        {
            existsItems = YES;
        }
    }
    if(existsItems)
    {
        [self.repairTypeSelectView showString:@"存在该维修类型的项目或零件，不能删除!" forSeconds:2];
        return NO;
    }
    return YES;
}
- (void)appointmentShowMessage:(NSNotification *)notification
{
    [self.view showString:@"VIN码还未输入!" forSeconds:1.5];
}

//快速建档 message
- (void)createFileMsgNotifacation:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSString *errMsg = dic[@"msg"];
    if(self.CreateFileView)
        [self.CreateFileView showString:errMsg forSeconds:1.5];
}

- (NSMutableArray *)VIN_FOR_ACTIVITY{
    [_VIN_FOR_ACTIVITY removeAllObjects];
    [_VIN_FOR_ACTIVITY PACA_AddObject:self.basicInfoView.VIN_FOR_ACTIVITY];
    return _VIN_FOR_ACTIVITY;
}

#pragma mark    返回按钮
- (void)backButtonClick{
    [self backButtonOrHomePageButtonAction:YES];
}

#pragma mark    返回首页
- (void)didbackHomePage{
    [self backButtonOrHomePageButtonAction:NO];
}

#pragma mark    返回按钮，判断是否有没有保存的信息
- (void)backButtonOrHomePageButtonAction:(BOOL)isBackButton{
    if (self.isEditType || self.isSave) {
        isBackButton?[self.navigationController popViewControllerAnimated:YES]:[self.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
    BOOL haveText = NO;
    if ([ICMPropertyManager isHaveTextInTextField:self.basicInfoView]) {
        haveText = YES;
    }
    if (self.activityView.selectedActivitys.count) {
        haveText = YES;
    }
    if (self.partsView.repairEventArray.count || self.partsView.repairPartsArray.count) {
        haveText = YES;
    }
    if (![self.partsView.partsRatioView.contentText isEqualToString:@"1.00"]||![self.partsView.workingRatioView.contentText isEqualToString:@"1.00"]) {
        haveText = YES;
    }
    if (haveText){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否放弃当前数据？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        alert.tag = 2233;
        [alert show];
    }else{
        isBackButton?[self.navigationController popViewControllerAnimated:YES]:[self.navigationController popToRootViewControllerAnimated:YES];
    }
}


- (MoreCarInfoView *)moreCarInfoView
{
    if(!_moreCarInfoView)
    {
        _moreCarInfoView = [[MoreCarInfoView alloc] init];
    }
    return _moreCarInfoView;
}

#pragma mark MoreCarInfoViewDelegate
//点击了那条数据
- (void)didClickedCellAtIndex:(NSInteger)index WithVIN:(NSString *)vin
{
    [self getInfoWithLICENSE:@"" orVin:vin];
    
    if(self.moreCarInfoView)
    {
        [self.moreCarInfoView removeFromSuperview];
        self.moreCarInfoView = nil;
    }
    
}

#pragma mark    获取活动数据
//参数 当前时间
//参数 VIN码
-(void)getActivity{
    NSString *VIN;
    if (self.VIN_FOR_ACTIVITY.count) {
        VIN = self.VIN_FOR_ACTIVITY[0];
    }
    if(!VIN || [VIN isEqualToString:@""])
    {
        [self.activityView.activityModels removeAllObjects];
        [self.activityView.selectedIndexs removeAllObjects];
        [self.activityView.activityTableView reloadData];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"AppointmentShowMessage" object:self];
        return;
    }
    
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:self.VIN_FOR_ACTIVITY[0] forKey:@"VIN"];
    [parameters setObject:self.activityView.stepInTime forKey:@"IN_TIME"];
    
    [manager startGetRequestWithURL:QureyActivity parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        
        if ([data[@"CODE"] integerValue] == 200) {
            NSArray *dataArray = data[@"TT_ACTIVITY"];

            if (dataArray.count != 0 && dataArray) {
                
            }else{
                if(self.actMsgBgView2)
                {
                    [self.actMsgBgView2 removeFromSuperview];
                }
                [self.activityView addSubview:self.actMsgBgView2];
                if(self.actMsgLabel2)
                {
                    [self.actMsgLabel2 removeFromSuperview];
                }
                [self.actMsgBgView2 addSubview:self.actMsgLabel2];
                
            }
        }
        else
        {
            if(self.actMsgBgView2)
            {
                [self.actMsgBgView2 removeFromSuperview];
            }
            [self.activityView addSubview:self.actMsgBgView2];
            if(self.actMsgLabel2)
            {
                [self.actMsgLabel2 removeFromSuperview];
            }
            [self.actMsgBgView2 addSubview:self.actMsgLabel2];
        }
        
        [self.activityView getActivityWithData:data];
    }];
}


//请求数据
#pragma mark    获取服务顾问可预约时间
- (void)getTimeDataWithDate:(NSString *)date andUserId:(NSString *)userId{
    NSDictionary *parameters = @{@"SERVICE_ADVISOR":userId,
                                 @"BOOKING_COME_TIME":date
                                 };
    
    [[[ICMRequestManager alloc]init]startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryBookingTimeInfo" parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        
        
        [self.basicInfoView.selecteStepInTimeView getTimeDataWithData:data WithDate:date];
    }];
}

#pragma mark    获得工位的可预约时间
- (void)getAllStationTimeDataWithDate:(NSString*)date{
    [[[ICMRequestManager alloc]init]startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryBookingLabourInfo" parameters:@{@"BOOKING_COME_TIME":date} inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        
        [self.basicInfoView.selecteStepInTimeView getAllStationTimeDataWithData:data];
    }];
}
#pragma mark    获得所有的服务顾问姓名和编码
- (void)getAllServerNameAndCode{
    [[[ICMRequestManager alloc]init]startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryServiceAdvisor" parameters:nil inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        [self.basicInfoView.selecteStepInTimeView getAllServerNameAndCodeWithData:data];
    }];
}

#pragma mark 快速建档回调查询
-(void)syncCustomInfoWithText:(NSString *)text isVin:(BOOL)isVin{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (isVin) {
        if (text && [ICrEAMTool isVinText:text]) {
            
            [parameters setObject:@"" forKey:@"LICENSE"];
            [parameters setObject:text forKey:@"VIN"];
        }else{
            [self.view showString:@"VIN码输入有误！" forSeconds:1.5];
            return;
        }
    }else{
        [parameters setObject:text forKey:@"LICENSE"];
        [parameters setObject:@"" forKey:@"VIN"];
    }
    
    [manager startGetRequestWithURL:QueryOwnerByVinLicense parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if ([data[@"DATA_STATUS"] integerValue] == 0){
                
                return ;
            }else{
                
                NSArray *carModelArray = data[@"TM_VEHICLE"];
                if (![carModelArray isKindOfClass:[NSArray class]]) {
                    return ;
                }
                CustomRecordModel *customModel=[[CustomRecordModel alloc]init];
                if (carModelArray.count>0) {
                    ICMCarModel *carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
                    customModel.VEHICLE_LIFE_STATUS=carModel.VEHICLE_LIFE_STATUS;
                    customModel.VIN=carModel.vinNum;
                    customModel.LICENSE=carModel.carNum;
                    customModel.BRAND_CODE=carModel.brand;
                    customModel.BRAND_NAME=carModel.brandName;
                    customModel.SERIES_CODE=carModel.carKei;
                    customModel.SERIES_NAME=carModel.carKeiName;
                    customModel.MODEL_CODE=carModel.carType;
                    customModel.MODEL_NAME=carModel.carTypeName;
                    customModel.BUSINESS_KIND=carModel.carNature;
                    customModel.SALES_DATE= [ICrEAMTool getDateWithTimeInterval:carModel.SALES_DATE];
                    customModel.OWNER_NO=carModel.OWNER_NO;
                    customModel.MILEAGE=carModel.lastMillions;
                    
                    if ([carModel.VEHICLE_LIFE_STATUS isEqualToString:@"25271001"]) {
                        [self alertMessage:@"该车辆为售前车辆，不支持预约和接车流程！" withBlock:^{
                            self.recordModel = nil;
                        }];
                        return;
                    }
                    
                    self.CreateFileView.hidden = NO;
                    self.CreateFileView.dateView.contentText=[ICrEAMTool getDateWithTimeInterval:carModel.SALES_DATE];
                    self.CreateFileView.natureView.contentText=[ICrEAMTool getCodeModelForCode:carModel.carNature].desc;
                    self.carModel=carModel;
                    if (![self.CreateFileView.selectAreaView.contentText isEqualToString:carModel.carNum]) {
                        
                        [self carChangeAlertMessage:@"车牌发生变化，选择“是”使用新车牌，选“否”选择老车牌！" withSuccessBlock:^{
                            
                            car_Num= self.CreateFileView.selectAreaView.contentText;
                            
                            customModel.LICENSE=self.CreateFileView.selectAreaView.contentText;
                            [self analysisData:data andmodel:customModel];
                            
                        } failBlock:^{
                            car_Num=@"";
                            
                            self.CreateFileView.selectAreaView.contentText=carModel.carNum;
                            [self analysisData:data andmodel:customModel];
                        }];
                    }else{
                        car_Num=@"";
                        [self analysisData:data andmodel:customModel];
                    }
                    
                    
                }
                /*
                 NSArray *personerModelArray = data[@"TM_OWNER"];
                 
                 if (![personerModelArray isKindOfClass:[NSArray class]]){
                 return;
                 }
                 if (personerModelArray.count>0) {
                 ICMPersonerModel *personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
                 customModel.GENDER=personerModel.gender;
                 customModel.PROVINCE=personerModel.province;
                 customModel.CITY=personerModel.city;
                 customModel.DISTRICT=personerModel.district;
                 customModel.OWNER_NAME=personerModel.ownerName;
                 customModel.CONTACTOR_NAME=personerModel.CONTACTOR_NAME;
                 customModel.CONTACTOR_MOBILE=personerModel.CONTACTOR_MOBILE;
                 customModel.MOBILE=personerModel.phoneNum;
                 customModel.OWNER_PROPERTY=personerModel.ownerType;
                 
                 self.CreateFileView.nameView.contentText=personerModel.ownerName;
                 self.CreateFileView.linkmanView.contentText=personerModel.CONTACTOR_NAME;
                 self.CreateFileView.linkmanMobelView.contentText=personerModel.CONTACTOR_MOBILE;
                 self.personerModel = personerModel;
                 }
                 [self.view showString:@"该车辆信息已存在" forSeconds:1.5];
                 */
                //                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
                //
                //                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                //                    [self.CreateFileView removeFromSuperview];
                //
                //                    [self getInfoWithLICENSE:@"" orVin:self.carModel.vinNum];
                //
                //                });
                
            }
        }
        
    }];
    
}
-(void)analysisData:(id)data andmodel:(CustomRecordModel*)customModel{
    NSArray *personerModelArray = data[@"TM_OWNER"];
    
    if (![personerModelArray isKindOfClass:[NSArray class]]){
        return;
    }
    if (personerModelArray.count>0) {
        ICMPersonerModel *personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
        customModel.GENDER=personerModel.gender;
        customModel.PROVINCE=personerModel.province;
        customModel.CITY=personerModel.city;
        customModel.DISTRICT=personerModel.district;
        customModel.OWNER_NAME=personerModel.ownerName;
        customModel.CONTACTOR_NAME=personerModel.CONTACTOR_NAME;
        customModel.CONTACTOR_MOBILE=personerModel.CONTACTOR_MOBILE;
        customModel.MOBILE=personerModel.phoneNum;
        customModel.OWNER_PROPERTY=personerModel.ownerType;
        
        self.CreateFileView.nameView.contentText=personerModel.ownerName;
        self.CreateFileView.linkmanView.contentText=personerModel.CONTACTOR_NAME;
        self.CreateFileView.linkmanMobelView.contentText=personerModel.CONTACTOR_MOBILE;
        self.personerModel = personerModel;
        self.recordModel = customModel;
    }
    [self.view showString:@"该车辆信息已存在" forSeconds:1.5];
    
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));
    
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        [self.CreateFileView removeFromSuperview];
        
        [self getInfoWithLICENSE:@"" orVin:self.carModel.vinNum];
        
    });
}

-(void)textFiledShouldReturnWithText:(NSString *)text isVin:(BOOL)isVin{
    [self syncCustomInfoWithText:text isVin:isVin];
}


-(NSString*)getTextFieldAreaAndPlate{

    //获取车牌号输入框的数据
    NSString *areaAndPlant = [NSString stringWithFormat:@"%@%@",self.basicInfoView.selectAreaView.contentText?:@"",self.basicInfoView.plateNumberView.contentText?:@""];

    return areaAndPlant;

}

-(NSString *)getTextFieldAreaStr{
    NSString *areaStr = self.basicInfoView.selectAreaView.contentText;
    return areaStr;
}

-(NSString *)getTextFieldPlateStr{
    NSString *plateStr = self.basicInfoView.plateNumberView.contentText;
    return plateStr;
}

-(NSString *)getModelAreaStr:(NSString *)AreaAndPlate{
    NSString *subAreaStr = [AreaAndPlate substringToIndex:1];
    return subAreaStr;
}
-(NSString *)getModelPlateStr:(NSString *)AreaAndPlate{
    NSString *subPlateStr = [AreaAndPlate substringFromIndex:1];
    return subPlateStr;
}

//快速建档车牌变化判断
-(void)judgeCreateViewState:(NSString *)textFieldAreaAndPlateStr{

    if ([ICrEAMTool isVinText:self.basicInfoView.vinNumberView.contentText]){
        //判断是否修改车牌
        if(IsStrEmpty(self.recordModel.LICENSE)){
        
        
        }else{
        
            if(![textFieldAreaAndPlateStr isEqualToString:self.recordModel.LICENSE]){
                
                [self carChangeAlertMessage:@"车牌发生变化，选择“是”使用新车牌，选“否”选择老车牌！" withSuccessBlock:^{
                    NSLog(@"是");
                    //用新车牌
                    self.carModel.carNum = textFieldAreaAndPlateStr;
                    
                } failBlock:^{
                    //复原车牌
                    if(IsStrEmpty(self.carModel.carNum)){
                        //快速建档中没有车牌时 输入车牌的回调取值
                        NSString *createSubAreaStr = [self getModelAreaStr:self.recordModel.LICENSE];
                        NSString *createPlateStr = [self getModelPlateStr:self.recordModel.LICENSE];
                        self.basicInfoView.selectAreaView.contentText = createSubAreaStr;
                        self.basicInfoView.plateNumberView.contentText = createPlateStr;
                        
                    }else{
                        NSString *modelAreaStr = [self getModelAreaStr:self.carModel.carNum];
                        NSString *modelPlateStr = [self getModelPlateStr:self.carModel.carNum];
                        self.basicInfoView.selectAreaView.contentText = modelAreaStr;
                        self.basicInfoView.plateNumberView.contentText = modelPlateStr;
                        
                    }}];
            }

        
        }
        
        
        
    }

    
    
}

//预约界面判断车牌变更
-(void)judgeAppointmentViewState:(NSString *)textFieldAreaAndPlateStr{
    if([ICrEAMTool isVinText:self.basicInfoView.vinNumberView.contentText]){
        //判断是否修改车牌
        if(![textFieldAreaAndPlateStr isEqualToString:self.carModel.carNum]){
            NSLog(@"改车牌了");
            [self carChangeAlertMessage:@"车牌发生变化，选择“是”使用新车牌，选“否”选择老车牌！" withSuccessBlock:^{
                NSLog(@"是");
                //用新车牌
                self.carModel.carNum = textFieldAreaAndPlateStr;
            } failBlock:^{
                //复原车牌
                if(IsStrEmpty(self.carModel.carNum)){
                    //快速建档中没有车牌时 输入车牌的回调取值
                    NSString *createSubAreaStr = [self.recordModel.LICENSE substringToIndex:1];
                    NSString *createPlateStr = [self.recordModel.LICENSE substringFromIndex:1];
                    self.basicInfoView.selectAreaView.contentText = createSubAreaStr;
                    self.basicInfoView.plateNumberView.contentText = createPlateStr;
                }else{
                    NSString *modelAreaStr = [self getModelAreaStr:self.carModel.carNum];
                    NSString *modelPlateStr = [self getModelPlateStr:self.carModel.carNum];
                    self.basicInfoView.selectAreaView.contentText = modelAreaStr;
                    self.basicInfoView.plateNumberView.contentText = modelPlateStr;
                    
                }}];
        }
    
    }//最外面的if括号
    
}
//预约界面判断存在车牌时输入vin获取车辆信息时判断车牌变化
-(void)judgeAppointmentViewStateOfExistAreaAndPlateStr:(NSString *)textFieldAreaAndPlateStr{
        //判断是否修改车牌
    if(IsStrEmpty(textFieldAreaAndPlateStr)){
        [self resetViewAfterSelectLicense];
    }else{
    
        if(![textFieldAreaAndPlateStr isEqualToString:self.carModel.carNum]){
            NSLog(@"改车牌了");
            [self carChangeAlertMessage:@"车牌发生变化，选择“是”使用新车牌，选“否”选择老车牌！" withSuccessBlock:^{
                NSLog(@"是");
                //用新车牌
                self.carModel.carNum = textFieldAreaAndPlateStr;
                [self resetViewAfterSelectLicense];
                
            } failBlock:^{
                
                [self resetViewAfterSelectLicense];
                
            }];
        }
        
    }
}





-(void)getMoreCarInfoViewWithLICENSE:(NSString *)LICENSE andCarArray:(NSArray *)carArray andData:(id)data andPersonerModelArray:(NSArray *)personerModelArray{

    //弹出多信息界面
    if (LICENSE && ![LICENSE isEqualToString:@""])
    {
        if(carArray.count > 1)
        {
            int i = 0;
            int index = -1;
            if(![self.basicInfoView.vinNumberView.contentText isEqualToString:@""])
            {
                for(i = 0;i<carArray.count;i++)
                {
                    ICMCarModel *tempCarModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][i]];
                    if([tempCarModel.vinNum isEqualToString:self.basicInfoView.vinNumberView.contentText])
                    {
                        break;
                    }
                }
                index = i;
            }
            else
            {
                index = -1;
            }
            if(i == carArray.count)
            {
                i = 0;
                index = -1;
            }
            self.carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][i]];
            self.personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][i]];
            
            self.moreCarInfoView.carArray = [[NSArray alloc] initWithArray:carArray];
            self.moreCarInfoView.personArray = [[NSArray alloc] initWithArray:personerModelArray];
            self.moreCarInfoView.selectedIndex = index;
            self.moreCarInfoView.delegate = self;
            //frame
            if(carArray.count>=6)
            {
                [self.moreCarInfoView resetViewFrame:CGRectMake(self.basicInfoView.plateNumberView.frame.origin.x, self.basicInfoView.plateNumberView.frame.origin.y + self.basicInfoView.plateNumberView.frame.size.height, 400, 44*6)];
            }
            else
            {
                [self.moreCarInfoView resetViewFrame:CGRectMake(self.basicInfoView.plateNumberView.frame.origin.x, self.basicInfoView.plateNumberView.frame.origin.y + self.basicInfoView.plateNumberView.frame.size.height, 400, 44*carArray.count)];
            }
            
            
            [self.basicInfoView addSubview:self.moreCarInfoView];
        }
        
    }


}


//请求车系工时单价
-(void)getLABOUR_PRICE:(NSString *)carModel{
    
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if(IsStrEmpty(carModel)){
        carModel = self.basicInfoModel.MODEL_CODE?:@"";
    }
    
    [parameters setObject:carModel forKey:@"MODEL_CODE"];
    [manager startGetRequestWithURL:queryLabourPrice parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            
            LABOUR_PRICE = data[@"LABOUR_PRICE"];
            self.partsView.SERIES_LABOUR_PRICE = LABOUR_PRICE;
            self.partsView.repairTypes = self.selectedRepairTypes;
            self.partsView.delegate = self;
            self.partsView.modelCode = carModel;
//            rself.partsTopModel = [[ICMPartsTopModel alloc]init];
//            self.partsTopModel.LABOUR_PRICE = LABOUR_PRICE;
            self.partsTopModel.SERIES_LABOUR_PRICE = LABOUR_PRICE;
            [self.partsView setupPartsViewWithPartsTopModel:self.partsTopModel];
        }
        
    }];
    
}
-(void)addReasonView{
    _reasonView = [[CancelViewController alloc]init];
    
    [self addChildViewController:_reasonView];
    [self.view addSubview:_reasonView.blankView];

}

@end
